import PIL
import face_recognition
import cv2  #openCV库
import numpy as np  #矩阵算法库
#原址：https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture_cnn.py
#这是一个从你的网络摄像头实时视频运行人脸识别的演示。这比
#另一个例子，但它包括一些基本的性能调整，使事情运行得更快：
# 1。以1/4分辨率处理每个视频帧（尽管仍以全分辨率显示）
# 2。只检测其他视频帧中的人脸。
#请注意：此示例要求只安装opencv（cv2库）以从网络摄像机中读取。
#使用人脸识别库时，不需要使用opencv。只有当你想运行这个时才需要它
#具体演示。如果您在安装它时遇到问题，请尝试其他不需要它的演示。

#获取对网络摄像机的引用0（默认值）
import time
from PIL import Image

from com.py.test.face_recogn.ReplaceFace.MyThread import MyThread
from com.py.test.face_recogn.ReplaceFace.ReplaceFaceDemo1 import test
video_capture = cv2.VideoCapture(0)

# 加载示例图片并学习如何识别它。
zhoujielun_image = face_recognition.load_image_file("D:/picture/face/李鸿武.jpg")
zhoujielun_face_encoding = face_recognition.face_encodings(zhoujielun_image)[0]

# # 加载第二个示例图片并学习如何识别它。
# huangjunkai_image = face_recognition.load_image_file("D:/picture/huangjunkai.jpg")
# huangjunkai_face_encoding = face_recognition.face_encodings(huangjunkai_image)[0]
#
# me_image = face_recognition.load_image_file("D:/picture/me.jpg")
# me_face_encoding = face_recognition.face_encodings(me_image)[0]
# me1_image = face_recognition.load_image_file("D:/picture/me1.jpg")
# me1_face_encoding = face_recognition.face_encodings(me1_image)[0]
# chenluliang_image = face_recognition.load_image_file("D:/picture/chenluliang.jpg")
# chenluliang_face_encoding = face_recognition.face_encodings(chenluliang_image)[0]
# chenweiliang_image = face_recognition.load_image_file("D:/picture/chenweiliang.jpg")
# chenweiliang_face_encoding = face_recognition.face_encodings(chenweiliang_image)[0]
# gujiahua_image = face_recognition.load_image_file("D:/picture/gujiahua.jpg")
# gujiahua_face_encoding = face_recognition.face_encodings(gujiahua_image)[0]

# 创建已知人脸编码及其名称的数组
known_face_encodings = [
    zhoujielun_face_encoding,
    # huangjunkai_face_encoding,
    # me_face_encoding,
    # me1_face_encoding,
    # chenluliang_face_encoding,
    # chenweiliang_face_encoding,
    # gujiahua_face_encoding
]
known_face_names = [
    "lihongwu1",
    # "HuangJunKai",
    # "Me",
    # "ChenLuQiang",
    # "ChenLuLiang",
    # "ChenWeiLiang",
    # "GuJiaHua"
]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    # 抓取一帧视频
    ret, frame = video_capture.read()

    # 将视频帧的大小调整为1/4，以便更快地进行人脸识别处理
    # small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    # 将图像从bgr颜色（opencv使用）转换为rgb颜色（人脸识别使用）
    # rgb_small_frame = frame[:, :, ::-1]
    # im = np.asarray(rgb_small_frame)
    # if np.all(im != 0):
    frame = test(frame)
    # else:
    #     print("ddddd")
    # Display the resulting image
    cv2.imshow('Video', frame)
    # 点击键盘上的“Q”退出！
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    time.sleep(1)
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()